﻿//*********************************************************
//
//    Copyright (c) Microsoft. All rights reserved.
//    This code is licensed under the Apache License, Version 2.0.
//    THIS CODE IS PROVIDED *AS IS* WITHOUT WARRANTY OF
//    ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING ANY
//    IMPLIED WARRANTIES OF FITNESS FOR A PARTICULAR
//    PURPOSE, MERCHANTABILITY, OR NON-INFRINGEMENT.
//
//*********************************************************

using System.ComponentModel;
using System.Data;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Controls.DataVisualization.Charting;
using System.Windows.Data;

namespace TridentChartActivities
{
    /// <summary>
    /// Interaction logic for TridentChartControl.xaml
    /// </summary>
    public partial class TridentChartControl : UserControl
    {
        /// <summary>
        /// The trident chart.
        /// </summary>
        private TridentChart chart;

        /// <summary>
        /// The data.
        /// </summary>
        private DataTable dataTable;

        /// <summary>
        /// Create the chart control.
        /// </summary>
        /// <param name="dataTable">
        /// The data.
        /// </param>
        /// <param name="allowChartTypeSelection">
        /// Is the user allowed to select the chart type.
        /// </param>
        public TridentChartControl(DataTable dataTable)
        {
            InitializeComponent();
            this.dataTable = dataTable;
            this.InitializeChart();
            this.chartTypeCombo.Loaded += new RoutedEventHandler(this.OnChartComboLoaded);
            this.chartTypeCombo.SelectionChanged += new SelectionChangedEventHandler(this.OnChartSelectionChanged);
        }

        /// <summary>
        /// Initializer.
        /// </summary>
        private void InitializeChart()
        {
            string independentValuePath = this.dataTable.Columns[0].ColumnName;
            string dependentValuePath = this.dataTable.Columns[1].ColumnName;

            this.DataContext = this.dataTable;
            this.chart = new TridentChart();
            this.grd.Children.Add(chart);

            AreaSeries aSeries = new AreaSeries();
            this.chart.AddSeries(aSeries, dependentValuePath, independentValuePath);

            BubbleSeries bSeries = new BubbleSeries();
            this.chart.AddSeries(bSeries, dependentValuePath, independentValuePath);

            ColumnSeries cSeries = new ColumnSeries();
            this.chart.AddSeries(cSeries, dependentValuePath, independentValuePath);

            LineSeries lSeries = new LineSeries();
            this.chart.AddSeries(lSeries, dependentValuePath, independentValuePath);

            PieSeries pSeries = new PieSeries();
            this.chart.AddSeries(pSeries, dependentValuePath, independentValuePath);

            ScatterSeries sSeries = new ScatterSeries();
            this.chart.AddSeries(sSeries, dependentValuePath, independentValuePath);
        }

        /// <summary>
        /// On the chart type selection combo loaded.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void OnChartComboLoaded(object sender, RoutedEventArgs e)
        {
            this.chartTypeCombo.ItemsSource = this.chart.ChartTypes;
            this.chartTypeCombo.SelectedIndex = 0;
        }

        /// <summary>
        /// On the chart type selection changed.
        /// </summary>
        /// <param name="sender">
        /// Chart type combo.
        /// </param>
        /// <param name="e">
        /// SelectionChangedEventArgs
        /// </param>
        private void OnChartSelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            this.chart.ShowSeries(e.AddedItems[0].ToString());
        }
    }
}
